package com.hwadee.property.controller.info;

import com.hwadee.property.pojo.UserActivityLog;
import com.hwadee.property.service.IUserActivityLogService;
import com.hwadee.property.util.AjaxResult;
import com.hwadee.property.dto.PageResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户活动日志控制器
 */
@RestController
@RequestMapping("/api/activity-logs")
@Api(tags = "用户活动日志-UserActivityLogController", description = "提供用户活动日志的增删查功能")
@CrossOrigin
public class UserActivityLogController {

    @Autowired
    private IUserActivityLogService activityLogService;

    /**
     * 分页获取用户活动日志列表
     * @param userId 用户ID
     * @param communityId 社区ID
     * @param userType 用户类型
     * @param logType 日志类型
     * @param currentPage 当前页码
     * @param pageSize 每页大小
     * @return 分页日志结果
     */
    @GetMapping("/page")
    @ApiOperation(value = "【管理员】分页获取用户活动日志列表", notes = "根据用户ID、社区ID、用户类型、关联记录ID以及分页参数查询日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "communityId", value = "社区ID", required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "userType", value = "用户类型", required = false, dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "logType", value = "日志类型", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "当前页码", required = true, defaultValue = "1", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, defaultValue = "10", dataType = "int", paramType = "query")
    })
    public AjaxResult getActivityLogsByPage(@RequestHeader("Authorization") String token,
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "communityId", required = false) Long communityId,
            @RequestParam(value = "userType", required = false) Integer userType,
            @RequestParam(value = "logType", required = false) String logType,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        PageResult<UserActivityLog> result = activityLogService.getActivityLogsByPage(
                userId, communityId, userType, logType, startTime, endTime, currentPage, pageSize);
        return AjaxResult.success(result);
    }

    /**
     * 获取用户活动日志列表
     * @param userId 用户ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 日志列表
     */
    @GetMapping
    @ApiOperation(value = "【管理员】根据用户ID和时间范围查询", notes = "根据用户ID和时间范围查询日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "string", paramType = "query")
    })
    public AjaxResult getActivityLogs(@RequestHeader("Authorization") String token,
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime) {
        List<UserActivityLog> logs;
        if (userId != null && startTime != null && endTime != null) {
            logs = activityLogService.getActivityLogsByUserIdAndTimeRange(userId, startTime, endTime);
        } else if (userId != null) {
            logs = activityLogService.getActivityLogsByUserId(userId);
        } else if (startTime != null && endTime != null) {
            logs = activityLogService.getActivityLogsByTimeRange(startTime, endTime);
        } else {
            logs = activityLogService.list(); // 返回所有日志
        }
        return AjaxResult.success(logs);
    }

    /**
     * 根据小区ID获取活动日志
     * @param communityId 小区ID
     * @return 日志列表
     */
    @GetMapping("/community/{communityId}")
    @ApiOperation(value = "【管理员】根据小区ID获取活动日志", notes = "查询指定小区的活动日志")
    @ApiImplicitParam(name = "communityId", value = "小区ID", required = true, dataType = "long", paramType = "path")
    public AjaxResult getActivityLogsByCommunityId(@RequestHeader("Authorization") String token, @PathVariable Long communityId) {
        List<UserActivityLog> logs = activityLogService.getActivityLogsByCommunityId(communityId);
        return AjaxResult.success(logs);
    }

/*  添加用户活动日志 实际由切面类 ..Aspect 执行

    @PostMapping
    @ApiOperation(value = "【管理员/自动】添加用户活动日志", notes = "创建新的用户活动日志记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userID", value = "用户 ID", required = true, dataType = "long", paramType = "path"),
            @ApiImplicitParam(name = "communityID", value = "小区 ID", required = true, dataType = "long", paramType = "path"),
            @ApiImplicitParam(name = "logType", value = "日志类型", required = true, dataType = "long", paramType = "path"),
            @ApiImplicitParam(name = "content", value = "日志内容详情", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "amount", value = "所涉金额", dataType = "double", paramType = "query"),
            @ApiImplicitParam(name = "relatedRecordID", value = "相关记录id", dataType = "long", paramType = "query")
    })
    public AjaxResult saveActivityLog(@RequestHeader("Authorization") String token,
            @ApiParam(value = "用户 ID", required = true) @RequestParam Long userID,
            @ApiParam(value = "小区 ID", required = true) @RequestParam Long communityID,
            @ApiParam(value = "日志内容详情", required = true) @RequestParam String content,
            @ApiParam(value = "日志类型", required = true) @RequestParam String logType,
            @ApiParam(value = "相关记录id", required = true) @RequestParam Long relatedRecordID,
            @ApiParam(value = "所涉金额") @RequestParam(required = false, defaultValue = "0.0") double amount) {
        boolean success = activityLogService.saveActivityLog(userID, communityID, content, amount, logType, relatedRecordID);
        return success ? AjaxResult.success() : AjaxResult.fail("添加日志失败");
    }*/

    /*
        不允许删除日志
     */
}
